﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mBook.Entity;
using JJY.CommonHelper;
using HtmlAgilityPack;

public partial class FetchData_BaiduTodayTop : System.Web.UI.Page
{
    private string EmptyString = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtUrl.Text = "http://top.baidu.com/buzz.php?p=book";
        }
    }

    public void btnFetch_OnClick(object sender, EventArgs e)
    {
        try
        {
            IList<BaiduTodayTop> topSet = new List<BaiduTodayTop>();

            string[] urlSet = txtUrl.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string url in urlSet)
            {
                topSet = topSet.Concat(GetData(url)).ToList();
            }

            for (int i = 0; i < topSet.Count; i++)
            {
                if (topSet[i] != null)
                {
                    topSet[i].TodayRanking = i + 1;
                }
            }

            //new DDWeekTopBS().DDWeekTopInsert(topSet);

            lblInfo.Text = "Fetch Successful!";
        }
        catch (Exception ex)
        {
            lblInfo.Text = ex.ToString();
        }
    }

    private IList<BaiduTodayTop> GetData(string url)
    {
        IList<BaiduTodayTop> result = new List<BaiduTodayTop>();

        string strHtml = HttpRequestService.GetHtmlSourceByUrl(url)
                .Replace(@"\t", EmptyString).Replace(@"\r", EmptyString).Replace(@"\n", EmptyString)
                .Replace("&nbsp;", EmptyString).Replace("　", EmptyString).Trim();

        HtmlDocument docList = new HtmlDocument();
        docList.LoadHtml(strHtml);


        HtmlNodeCollection nodeTrSet = docList.DocumentNode.SelectNodes("//div[@class='list']/table/tbody/tr");

        int todayRanking = 0;
        string bookName = string.Empty;
        string author = string.Empty;
        string press = string.Empty;
        DateTime pressDate = new DateTime(1900, 1, 1);
                
        foreach (HtmlNode nodeTr in nodeTrSet)
        {
            if (nodeTr != null)
            {
                HtmlNode nodeTh = docList.DocumentNode.SelectNodes(nodeTr.XPath + "/td").FirstOrDefault();
                if (nodeTh != null)
                {
                    int.TryParse(nodeTh.InnerText, out todayRanking);
                }

                HtmlNodeCollection nodeTdSet = docList.DocumentNode.SelectNodes(nodeTr.XPath + "/td");
                if (nodeTdSet[5] != null)
                {
                    HtmlNode nodeLink = docList.DocumentNode.SelectNodes(nodeTdSet[5].XPath + "/a")[2];
                    string urlDetail = nodeLink.Attributes["href"].Value;
                    string strBookDetail = HttpRequestService.GetHtmlSourceByUrl(url)
                                .Replace(@"\t", EmptyString).Replace(@"\r", EmptyString).Replace(@"\n", EmptyString)
                                .Replace("&nbsp;", EmptyString).Replace("　", EmptyString).Trim();

                    HtmlDocument docDetail = new HtmlDocument();
                    docDetail.LoadHtml(strBookDetail);

                    HtmlNodeCollection nodeDetailTdSet =
                        docDetail.DocumentNode.SelectNodes("td[@class='seperateTd']");
                    if (nodeDetailTdSet != null)
                    {
                        HtmlNodeCollection nodeDetailTd1 =
                            docDetail.DocumentNode.SelectNodes(nodeDetailTdSet[0].XPath + "/table/tbody/tr/td");
                        HtmlNodeCollection nodeDetailTd2 =
                            docDetail.DocumentNode.SelectNodes(nodeDetailTdSet[1].XPath + "/table/tbody/tr/td");
                        
                        bookName = nodeDetailTd1[1].InnerText;
                        author = nodeDetailTd1[3].InnerText;
                        press = nodeDetailTd1[7].InnerText;
                        DateTime.TryParse(nodeDetailTd2[7].InnerText, out pressDate);
                    }

                }

                result.Add(new BaiduTodayTop()
                {
                    TodayRanking = todayRanking,
                    BookName = bookName,
                    Author= author,
                    Press = press
                });
            }
        }

        return result;
    }
}